// pages/points/points.js
const app = getApp()
const { getUserAllPointsChangeList } = require("../../utils/api");
Page({

  /**
   * 页面的初始数据
   */ 
  data: {
    header: {
      title: '积分明细',
      whiteBg: false,
      mode: 'back',
      navBarHeight: app.globalData.navBarHeight
    },

    active:0,
    userInfo: null,
    list:[
      {
        name:"全部",
        id:"0"
      },
      {
        name:"支出",
        id:"1"
      },
      {
        name:"预收",
        id:"2"
      },
      {
        name:"收入",
        id:"3"
      }
    ],
    pointsChangeList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  changeTab(e){
    console.log(e.currentTarget.dataset.id.id)
    const id =  e.currentTarget.dataset.id.id
    let pointsChangeList = this.data.pointsChangeList
    switch (Number(id)) {
      case 0:
        pointsChangeList.forEach(item => {
          item.show = true
        })
          break;
      case 1:
          pointsChangeList.forEach(item => {
            if(item.points < 0) {item.show = true} else {item.show = false}
          })
          break;
      case 2:
        pointsChangeList.forEach(item => {
          if(item.reservedPoints > 0) {item.show = true} else {item.show = false}
        })
        break;
      case 3:
        pointsChangeList.forEach(item => {
          if(item.points > 0) {item.show = true} else {item.show = false}
        })
        break;
      // 可以有任意数量的 case 语句
      default:
          return
    }
    this.setData({
      active:id,
      pointsChangeList: pointsChangeList
    })
  },
  onLoad: function (options) {
    this.setData({
      userInfo: wx.getStorageSync('userInfo')
    })
    const that = this; 
    getUserAllPointsChangeList({
      PhoneNumber: this.data.userInfo.phoneNumber // 请求 body 数据
    }).then(res => {
      if (res.code === 4003010) {
        that.setData({ // 使用之前保存的 this
          pointsChangeList: res.data.map(item => {
            return {
              ...item,
              show: true
            }
          })
        });
      } else {
        console.error('请求失败，错误码：', res.code);
      }
    });
  },

  onPageScroll(e) {
    this.setData({
      'header.whiteBg': e.scrollTop < 40 ? false : true
    })
  },
})